<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
 
    	Zend_Loader::loadClass('UserModel');
    	Zend_Loader::loadClass('SuggestModel');
    }

    public function indexAction()
    {	
    	$this->view->headLink()->appendStylesheet('/css/login.css');
    	$this->view->headScript()->appendFile('/js/login.js');
    	$this->view->register = $this->_request->getParam("register");
    	$auth = Zend_Auth::getInstance();
    	if( $auth->hasIdentity() ) {
    		$username = $auth->getIdentity()->username;
     		$this->_redirect("/".$username."/wall");
     	}
    }
    public  function checkAction(){
    	$username = $_POST['username'];
    	if( count(  UserModel::check($username) )>0 ) echo '1';
    	else echo '0';
    	exit();
    }
	public function registerAction()
    {
    	$username=$_POST['username'];
    	$password = $_POST['pass'];
    	$fullname = $_POST['fullname'];
    	$avatarTmp = $_FILES['avatar']['name'];
    	$music = $_POST['music'];
    	$sport = $_POST['sport'];
    	$address = $_POST['address'];
    	$email = $_POST['email'];
    	$sex = $_POST['sex'];
    	$birthday = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
    	
    	$addressSuggest = SuggestModel::getAddress();
    	$addressId = count( $addressSuggest );
    	foreach ( $addressSuggest as $val ){
    		if( $address == $val->name ){
    			$addressId = $val->id;
    			break;
    		}
    	}
    	
    	$image_tempname = $_FILES['avatar']['name'];
    	$ImageDir =IMAGE_PATH."/avatar/";
    	$avatar = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5)),0,5).$avatarTmp;
		$ImageName = $ImageDir . $avatar;
		if(move_uploaded_file($_FILES['avatar']['tmp_name'],
			$ImageName)) 
		{
			list($width, $height, $type, $attr) = getimagesize($ImageName);
			switch ($type)
 			{
			case 1:
				$ext = ".gif";
				break;
			case 2:
				$ext = ".jpg";
				break;
			case 3:
				$ext = ".png";
				break;
			default:
				$avatar="default.jpg";
			}
    	
		
    	}else {
    		$avatar="default.jpg";
    	}
    	UserModel::register($username, $password, $fullname,$email, $avatar, $music, $sport, $addressId, $sex, $birthday);
    	$this->_redirect("/befriend/index/index?register=1");
    	exit();
    }
    
    public function loginAction(){
    	$db = Zend_Registry::get('connectDB');
    	 $auth = Zend_Auth::getInstance();
    	 $authAdapter = new Zend_Auth_Adapter_DbTable($db);
    	 $authAdapter->setTableName('user')->setIdentityColumn("username")->setCredentialColumn("password");
    	 
    	 $value= $_POST['user'];
    	 $pass = $_POST['pass_login'];
    	 $select = $authAdapter->getDbSelect();
    	 $authAdapter->setIdentity($value);
    	 $authAdapter->setCredential($pass);
    	 
    	 $result = $auth->authenticate( $authAdapter);
    	 
    	 if( $result->isValid() ){
    	 	$col = array('id', 'username', 'password', 'email', 'fullname', 'avatar', 'music', 'sex', 'sport');
    		$data = $authAdapter->getResultRowObject();
    		$auth->getStorage()->write( $data);
    		$user_id = $auth->getIdentity()->id;
    	 	if ($this->_request->getParam('remember') == 'on'){
    			
    			require_once 'Zend/Session.php';
    			$time = 60 * 60 * 24 * 7 * 30;
    			Zend_Session::rememberMe($time);
    		}else {
    			Zend_Session::forgetMe();
    		}
    		$this->_redirect("/".$auth->getIdentity()->username."/wall");
    	 	exit();
    	 } else {
    	 	$this->_redirect('/');
    	 	exit();
    	 }
    }
  	function logoutAction() {
    	$auth = Zend_Auth::getInstance();
    	$auth->clearIdentity();
    	$this->_redirect("/");
    }
    
 	public function addressAction()
    {
		$address = $_POST['address'];
		$addressSuggest = SuggestModel::getAddress();
	
		if( strlen( $address ) == 0 ) exit();
		for($i=1; $i<count($addressSuggest ); $i++) 
		{ 
			if (strtolower($address) == strtolower(substr($addressSuggest[ $i ]->name,0,strlen($address)))) 
			{ 
			$result[]=$addressSuggest[ $i ]->name;
			}
		}
	
		$res = "";
		if( count( $result ) > 0 ) {
			$i = 0;
			foreach( $result as $add ){
				$res=$res."-".$add;
			}
		}
		else {
			$res = "-other";
		}
		echo $res;
    	
    	exit();


	}
	
	public function sportAction(){
		$sport = $_POST['sport'];
		$sportSuggest[]="Football";
		$sportSuggest[]="Badminton";
		$sportSuggest[]="Baseket";
		$sportSuggest[]="Golf";
		$sportSuggest[]="Tenis";
		$sportSuggest[]="Table Tenis";
		
		
	
		if( strlen( $sport ) == 0 ) exit();
		for($i=0; $i<count($sportSuggest); $i++) 
		{ 
			if (strtolower($sport) == strtolower(substr($sportSuggest[$i],0,strlen($sport)))) 
			{ 
				$result[]=$sportSuggest[ $i ];
			}
		}
	
		$res = "";
		if( count( $result ) > 0 ) {
			foreach( $result as $add ){
				$res=$res."-".$add;
			}
		}
		else {
			$res = "-Other";
		}
		echo $res;
		exit();
	}
	
public function musicAction(){
		$music = $_POST['music'];
		$musicSuggest[]="Clasic";
		$musicSuggest[]="Pop";
		$musicSuggest[]="R&B";
		$musicSuggest[]="Dance";
		$musicSuggest[]="Remix";
		
		if( strlen( $music ) == 0 ) exit();
		for($i=0; $i<count( $musicSuggest ); $i++) 
		{ 
			if (strtolower($music) == strtolower(substr( $musicSuggest [$i],0,strlen($music)))) 
			{ 
				$result[]= $musicSuggest[ $i ];
			}
		}
	
		$res = "";
		if( count( $result ) > 0 ) {
			$i = 0;
			foreach( $result as $add ){
				$res=$res."-".$add;
			}
		}
		else {
			$res = "-Other";
		}
		echo $res;
		exit();
	}
}

